using System.Collections.Generic;
using ProtoModels;

public class FriendProfilesManager : BehaviourSingletonInit<FriendProfilesManager>
{
	public delegate void OnFreshFriendProfilesAvailable();

	public List<FriendProfileConfig.FriendProfileWrapper> friendsProfiles { get; protected set; }

	public List<FriendProfileConfig.FriendProfileWrapper> allProfiles { get; protected set; }

	public FriendProfileConfig.FriendProfileWrapper myProfile { get; protected set; }

	public int teamCount
	{
		get
		{
			if (allProfiles == null)
			{
				return 0;
			}
			return friendsProfiles.Count;
		}
	}

	public string PlayerName
	{
		get
		{
			return (myProfile == null) ? GGPlayerSettings.instance.GetName() : GGFormat.FirstName(myProfile.playerName);
		}
	}

	public event OnFreshFriendProfilesAvailable onProfilesAvailable;

	public override void Init()
	{
		friendsProfiles = new List<FriendProfileConfig.FriendProfileWrapper>();
		allProfiles = new List<FriendProfileConfig.FriendProfileWrapper>();
		BehaviourSingleton<GGNotificationCenter>.instance.onMessage += OnMessageDelegate;
	}

	private void OnMessageDelegate(string message)
	{
		if ("MessageConflictResolved".Equals(message) && myProfile != null)
		{
			myProfile.careerScore = SingletonInit<PlayerCareer>.Instance.scoreDAO;
		}
	}

	public void RefreshFriendProfiles()
	{
		if (!GGPlayerSettings.instance.Model.hasLoggedIntoFacebook)
		{
			myProfile = null;
			friendsProfiles.Clear();
			allProfiles.Clear();
			if (this.onProfilesAvailable != null)
			{
				this.onProfilesAvailable();
			}
		}
		else
		{
			GGServerRequestsBackend.GGServerGetFriendProfiles gGServerGetFriendProfiles = new GGServerRequestsBackend.GGServerGetFriendProfiles(FriendProfileConfig.instance.GetProfileFiles());
			gGServerGetFriendProfiles.cache = CacheStategy.CacheToFile;
			gGServerGetFriendProfiles.cacheTimeToLive = GGRequestCache.InfiniteTimeToLive();
			gGServerGetFriendProfiles.cacheGetStrategy = CacheGetStrategy.GetFromCacheOnlyIfRequestFails;
			BehaviourSingletonInit<GGServerRequestsBackend>.instance.GetFriendProfiles(gGServerGetFriendProfiles, OnRefreshProfilesFinished);
		}
	}

	public void OnRefreshProfilesFinished(GGServerRequestsBackend.ServerRequest request)
	{
		if (request.status != 0)
		{
			if (GGSupportMenu.instance.isNetworkConnected() && request.errorMessage == ConfigBase.instance.facebookError)
			{
				GGPlayerSettings.instance.Model.hasLoggedIntoFacebook = false;
				GGPlayerSettings.instance.Save();
				myProfile = null;
				friendsProfiles.Clear();
				allProfiles.Clear();
				if (this.onProfilesAvailable != null)
				{
					this.onProfilesAvailable();
				}
			}
			return;
		}
		FriendsProfiles response = request.GetResponse<FriendsProfiles>();
		myProfile = new FriendProfileConfig.FriendProfileWrapper(SingletonInit<PlayerCareer>.Instance.scoreDAO, response.myName, response.myId, response.myPicture.data.url);
		friendsProfiles.Clear();
		allProfiles.Clear();
		allProfiles.Add(myProfile);
		foreach (FriendsProfiles.FriendProfileData profile in response.profiles)
		{
			FriendProfileConfig.FriendProfileWrapper item = new FriendProfileConfig.FriendProfileWrapper(profile);
			friendsProfiles.Add(item);
			allProfiles.Add(item);
		}
		if (this.onProfilesAvailable != null)
		{
			this.onProfilesAvailable();
		}
	}
}
